//
//  KKRankController.m
//  DoMi
//
//  Created by 康凯 on 2016/11/9.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKRankController.h"
#import "UIColor+Hex.h"
#import "KKVideo1Cell.h"
#import "KKRankRuleController.h"
#import "KKStudyHttp.h"
#import "KKVideoDetailController.h"
#import "KKArticleDetailVC.h"
#import "KKArticalCell.h"
#import "KKPersonalHomeVC.h"

@interface KKRankController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak,nonatomic) UITableView *tableView;
@property (strong,nonatomic) NSArray *titles;
@property (strong,nonatomic) UIButton *tempBtn;

@property (copy,nonatomic) NSString *type;
@property (strong,nonatomic)NSArray *rankDatas;
@end

@implementation KKRankController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBlack];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initNav];
    [self initView];
}
-(void) initNav{
    self.view.backgroundColor=[UIColor colorWithHexString:@"#EAEBEC"];
    self.navigationItem.title=@"排行榜";
    //右侧规则
    UIButton *rightBtn = [[UIButton alloc ]initWithFrame:CGRectMake(0, 0, 50, 30)];
    [rightBtn setImage:[UIImage imageNamed:@"rules"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rule) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
}

-(void)initView{
    self.titles=@[@"视频",@"文章",@"专家",@"董秘",@"新人",];
    self.type=@"v";
    
    UIView *topView=[[UIView alloc] initWithFrame:CGRectMake(0, margin10, screenWidth, 35)];
    topView.backgroundColor=[UIColor whiteColor];
    for(int i=0;i<self.titles.count;i++){
        float itemWidth=screenWidth/self.titles.count;
        UIButton *item=[[UIButton alloc] initWithFrame:CGRectMake(i*itemWidth, 0, itemWidth, 35)];
        if(i==0){
            self.tempBtn=item;
        }
        item.selected=i==0?YES:NO;
        item.tag=i;
        [item setTitle:self.titles[i] forState:UIControlStateNormal];
        [item setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [item setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        item.titleLabel.font=[UIFont systemFontOfSize:14];
        [item addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [topView addSubview:item];
    }
    [self.view addSubview:topView];
    
    float topItemHeight=CGRectGetMaxY(topView.frame)+margin10;
    UITableView *tableView=[[UITableView alloc] initWithFrame:CGRectMake(margin5,topItemHeight, screenWidth-margin10, screenHeight-topItemHeight-margin10-self.topHeight) style:UITableViewStylePlain];
    self.tableView=tableView;
    self.tableView.backgroundColor=[UIColor colorWithHexString:@"#EAEBEC"];
    tableView.delegate=self;
    tableView.dataSource=self;
    tableView.showsVerticalScrollIndicator=NO;
    tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"KKVideo1Cell" bundle:nil] forCellReuseIdentifier:@"KKVideo1Cell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"KKArticalCell" bundle:nil] forCellReuseIdentifier:@"KKArticalCell"];
    
    
    [self getRankList];
}

#pragma mark 头部item点击
-(void)btnClick:(UIButton *)btn{
    self.tempBtn.selected=NO;
    btn.selected=YES;
    self.tempBtn=btn;
    switch (btn.tag) {
        case 0:
            self.type=@"v";
            break;
        case 1:
            self.type=@"w";
            break;
        case 2:
            self.type=@"e";
            break;
        case 3:
            self.type=@"董秘";
            break;
        case 4:
            self.type=@"新人";
            break;
    }
    [self getRankList];
}
#pragma mark 规则点击
-(void)rule{
    KKRankRuleController *rule=[[KKRankRuleController alloc] init];
    [self.navigationController pushViewController:rule animated:YES];
}

#pragma mark tableview delegate
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if([self.type isEqualToString:@"v"]){
        KKVideo1Cell *video=(KKVideo1Cell *)cell;
        video.playCount.hidden=NO;
        if(indexPath.row<5){
            video.rankImg.hidden=NO;
        }else{
            video.rankImg.hidden=YES;
        }
        [video initRankData:self.rankDatas[indexPath.row]];
    }else if([self.type isEqualToString:@"w"]){
        KKVideo1Cell *video=(KKVideo1Cell *)cell;
        video.playCount.hidden=NO;
        if(indexPath.row<5){
            video.rankImg.hidden=NO;
        }else{
            video.rankImg.hidden=YES;
        }
        [video initArticleRankData:self.rankDatas[indexPath.row]];
    }else if([self.type isEqualToString:@"e"]){
        KKVideo1Cell *video=(KKVideo1Cell *)cell;
        video.playCount.hidden=NO;
        if(indexPath.row<5){
            video.rankImg.hidden=NO;
        }else{
            video.rankImg.hidden=YES;
        }
        [video initRankExpertData:self.rankDatas[indexPath.row]];
    }else{
        KKVideo1Cell *video=(KKVideo1Cell *)cell;
        video.playCount.hidden=NO;
        if(indexPath.row<5){
            video.rankImg.hidden=NO;
        }else{
            video.rankImg.hidden=YES;
        }
        [video initOtherData:self.rankDatas[indexPath.row]];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.rankDatas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell;
    if([self.type isEqualToString:@"v"]){
        cell= [tableView dequeueReusableCellWithIdentifier:@"KKVideo1Cell"];
    }else if([self.type isEqualToString:@"w"]){
        cell= [tableView dequeueReusableCellWithIdentifier:@"KKVideo1Cell"];
    }else if([self.type isEqualToString:@"e"]){
        cell= [tableView dequeueReusableCellWithIdentifier:@"KKVideo1Cell"];
    }else{
        cell= [tableView dequeueReusableCellWithIdentifier:@"KKVideo1Cell"];
    }
    cell.backgroundColor=[UIColor colorWithHexString:@"#EAEBEC"];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KKStudyList *list= self.rankDatas[indexPath.row];
    if([self.type isEqualToString:@"v"]){
        KKVideoDetailController *detail=[[KKVideoDetailController alloc]init];
        detail.videoId=list.videoid;
        [self.navigationController pushViewController:detail animated:YES];
    }else if([self.type isEqualToString:@"w"]){
        KKArticleDetailVC *detail=[[KKArticleDetailVC alloc] init];
        detail.docId=list.articleid;
        [self.navigationController pushViewController:detail animated:YES];
    }else{
        KKPersonalHomeVC *person=[[KKPersonalHomeVC alloc] init];
        person.expertId=list.uid;
        person.flag=1;
        [self.navigationController pushViewController:person animated:YES];
    }
}

#pragma mark 接口--排行榜数据
-(void)getRankList{
    [self showDialog];
    [KKStudyHttp getRankList:self.type success:^(id datas) {
        self.rankDatas=datas;
        [self.tableView reloadData];
        [self dismissDialog];
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}
@end
